<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
date_default_timezone_set ( 'prc' );
class Message extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		if (! $this->session->userdata ( 'logged_in' )) {
			redirect ( 'passport/index' );
			exit ();
		}
	}
	/*
	 * 返回通知列表
	 */
	public function index() {
		$this->load->library ( 'pagination' );
		// 每页显示数据条数
		$per_page=20;//暂定为20条

		$config['base_url']=site_url('message/index');
		$this->load->model ( 'messagerecord_model' );
		// 数据总数
		$config ['total_rows'] = $this->messagerecord_model->count_records();
		// $config ['total_rows']=200;
		$config ['per_page'] = $per_page;
		$config ['first_link'] = '第一页';
		$config ['last_link'] = '最后一页';
		$config ['next_link'] = '下一页';
		$config ['prev_link'] = '上一页';
		$config['uri_segment'] =3;

		$this->pagination->initialize($config);
		$data['links']=$this->pagination->create_links();
		// 偏移量
		$offset=intval($this->uri->segment(3));
		$data ['records']=$this->messagerecord_model->get_records ($per_page,$offset);
		$data ['title'] = '通知列表 - '.SYSTEM_NAME;
		$this->load->view ( 'header', $data );
		$this->load->view ( 'message/index' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 跳转到发送通知页面
	 */
	public function add() {
		$data['title'] = '发送通知 - '.SYSTEM_NAME;
		$this->load->view ( 'header', $data );
		$this->load->view ( 'message/add');
		$this->load->view ( 'footer');
	}
	/*
	 * 跳转到编辑通知页面
	 */
	public function edit() {
		$id = $this->uri->segment(4);
		$this->load->model ( 'messagerecord_model' );
		$message = $this->messagerecord_model->load ( $id );
		$data['message']=$message;
		$data['title'] = '编辑通知 - '.SYSTEM_NAME;
		$this->load->view ( 'header', $data );
		$this->load->view ( 'message/edit');
		$this->load->view ( 'footer');
	}
	/*
	 * 保存通知
	 */
	public function save() {
		$adminid=$this->session->userdata ( 'adminid' );
		$id=$this->input->post ( 'id' );
		$title=$this->input->post ( 'title' );
		$content=$this->input->post ( 'content' );
		$record = array (
			'title' => $title,
			'content' => $content,
			'time' => date ( "Y-m-d H:i:s" ),
			'adminid' => $adminid
		);
		$this->load->model ( 'user_model' );
		$this->load->model ( 'messagerecord_model' );
		if($id){
			$this->messagerecord_model->update ( $id, $record );
			return_json(TRUE,'','修改成功！');
		}else{
			$recordid=$this->messagerecord_model->create ( $record );
			if(!empty($recordid)){
				return_json(TRUE,'','保存成功!');
			}
			return_json(FALSE,'','保存失败!');
		}
	}
	/*
	 * 删除通知信息
	 */
	public function delete() {
		$id = $this->input->post('id');
		$this->load->model ( 'messagerecord_model' );
		$newmessage = array (
    		'del' => 1
    	);
		if ($this->messagerecord_model->update ( $id, $newmessage )) {
			return_json(TRUE,'','删除成功！');
	    }else{
	    	return_json(FALSE,'','删除失败！');
	    }
	}
}
